package chaoyue.study.thread;

/**
 * 线程让步
 * Thread.yield方法可以当当前线程主动放弃cpu的时间片交给其他线程
 */
public class ThreadYield {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            String threadName = Thread.currentThread().getName();
            for (int i = 0; i < 1000; i++) {
                System.out.println(threadName + " --> " + i);
                // 线程让步 让位给其他线程执行
                Thread.yield();
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            String threadName = Thread.currentThread().getName();
            for (int i = 0; i < 1000; i++) {
                System.out.println(threadName + " --> " + i);
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
